﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Windows.Controls;
using Microsoft.Practices.Prism.Commands;
using Infrastructure;
using DBDataAccessLib;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OracleClient;

namespace SystemParametersConfiguration.ViewModel
{
    public class SystemParaConfigurationViewModel : Infrastructure.ViewModelBase
    {
        public SystemParaConfigurationViewModel()
        {
            this.ImageServerUrl = Helper.ServerUrl;
            this.ConnectionString = Helper.ConnectionString;
            this.IsAutoPlayChecked = Helper.GetConfigurationValue("DisplayOptions") == "AutoPlay" ? true : false;
            this.IsManuallyPlayChecked = Helper.GetConfigurationValue("DisplayOptions") == "AutoPlay" ? false : true;
            this.WCNOLine = Helper.GetConfigurationValue("WCNO");
            this.StationName = Helper.GetConfigurationValue("StationName");
            this.PortNumber = Helper.GetConfigurationValue("PortNumber");
            this.IsPortScanChecked = Helper.GetConfigurationValue("ScanOptions") == "PortScan" ? true : false;
            this.IsRFIDScanChecked = Helper.GetConfigurationValue("ScanOptions") == "PortScan" ? false : true;
            this.SelectedTheme = Helper.GetConfigurationValue("Theme");
            Themes = new List<string>()
            {
                "Default", "Vista","Windows7","Metro","Office Blue","Office Black",
                "Office Silver","Transparent","Summner","Expression Dark"
            };

            SaveCommand = new DelegateCommand<object>(OnSaveCommand);
        }

        public DelegateCommand<object> SaveCommand { get; set; }

        private List<string> themes;

        public List<string> Themes
        {
            get { return themes; }
            set
            {
                themes = value;
                OnPropertyChanged("Themes");
            }
        }

        private string selectedTheme;

        public string SelectedTheme
        {
            get { return selectedTheme; }
            set
            {
                if (selectedTheme != value)
                {
                    selectedTheme = value;
                    OnPropertyChanged("SelectedTheme");
                }
            }
        }

        public void OnSaveCommand(object obj)
        {
            SetConnectionString();

            Helper.UpdateAppSetting("//appSettings/add[@key='ImageServerURI']", ImageServerUrl);
            Helper.UpdateAppSetting("//appSettings/add[@key='DisplayOptions']", IsAutoPlayChecked ? "AutoPlay" : "ManuallyPlay");
            Helper.UpdateAppSetting("//appSettings/add[@key='WCNO']", WCNOLine);
            Helper.UpdateAppSetting("//appSettings/add[@key='StationName']", StationName);
            Helper.UpdateAppSetting("//appSettings/add[@key='PortNumber']", PortNumber);
            Helper.UpdateAppSetting("//appSettings/add[@key='ScanOptions']", IsPortScanChecked ? "PortScan" : "RFIDScan");
            Helper.UpdateAppSetting("//appSettings/add[@key='Theme']", SelectedTheme);
            ModalWindowService.ShowMessage("保存成功。");
        }

        private void SetConnectionString()
        {
            try
            {
                bool success = TestConnectionCore();
                if (success)
                {
                    string xpath = "//connectionStrings/add[@name='" + (DBHelperFactory.DbName == DBType.Oracle ? "OraConnString1" : "SQLConnString1") + "']";
                    Helper.UpdateNode(xpath, "connectionString", ConnectionString);
                }
                else
                {
                    ModalWindowService.ShowMessage("不能连接到数据库。连接字符串保存失败。");
                }
            }
            catch (Exception ex)
            {
                ModalWindowService.ShowMessage("不能连接到数据库。连接字符串保存失败。\r\n" + ex.Message);
            }
        }

        private bool TestConnectionCore()
        {
            DbConnection cn = null;
            if (DBHelperFactory.DbName == DBType.SqlServer)
            {
                cn = new SqlConnection(ConnectionString);
            }
            else cn = new OracleConnection(ConnectionString);
            cn.Open();
            if (cn.State.ToString().ToUpper() == "OPEN") return true;
            else return false;
        }

        private string imageServerUrl;

        public string ImageServerUrl
        {
            get { return imageServerUrl; }
            set
            {
                imageServerUrl = value.Trim();
                OnPropertyChanged("ImageServerUrl");
            }
        }

        private string connectionString;

        public string ConnectionString
        {
            get { return connectionString; }
            set
            {
                connectionString = value.Trim();
                OnPropertyChanged("ConnectionString");
            }
        }

        private bool isAutoPlayChecked;

        public bool IsAutoPlayChecked
        {
            get { return isAutoPlayChecked; }
            set
            {
                isAutoPlayChecked = value;
                OnPropertyChanged("IsAutoPlayChecked");
            }
        }

        private bool isManuallyPlayChecked;

        public bool IsManuallyPlayChecked
        {
            get { return isManuallyPlayChecked; }
            set
            {
                isManuallyPlayChecked = value;
                OnPropertyChanged("IsManuallyPlayChecked");
            }
        }

        private string wcnoLine;

        public string WCNOLine
        {
            get { return wcnoLine; }
            set
            {
                wcnoLine = value.Trim();
                OnPropertyChanged("WCNOLine");
            }
        }

        private string stationName;

        public string StationName
        {
            get { return stationName; }
            set
            {
                stationName = value.Trim();
                OnPropertyChanged("StationName");
            }
        }

        private string portNumber;

        public string PortNumber
        {
            get { return portNumber; }
            set
            {
                portNumber = value.Trim();
                OnPropertyChanged("PortNumber");
            }
        }

        private bool isPortScanChecked;

        public bool IsPortScanChecked
        {
            get { return isPortScanChecked; }
            set
            {
                isPortScanChecked = value;
                OnPropertyChanged("IsPortScanChecked");
            }
        }

        private bool isRFIDScanChecked;

        public bool IsRFIDScanChecked
        {
            get { return isRFIDScanChecked; }
            set
            {
                isRFIDScanChecked = value;
                OnPropertyChanged("IsRFIDScanChecked");
            }
        }

    }
}
